require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIDropInfo = {};
HelperFunc.SetUIEnv(UIDropInfo)
local this = UIDropInfo;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.btnBgClose = nil;
this.txtTitle = nil;
this.imgPlayExpIcon = nil;
this.txtPlayExpName = nil;
this.txtPlayExpOutput = nil;
this.imgHeroExpIcon = nil;
this.txtHeroExpName = nil;
this.txtHeroExpOutput = nil;
this.imgGoldIcon = nil;
this.txtGoldName = nil;
this.txtGoldtOutput = nil;
this.listitemGrid = nil;
this.listNextStageItemGrid = nil;
this.listData = nil;

this.mapConfig = nil;
this.itemConfig = nil;
this.dropConfig = nil;
this.equipConfig = nil;
this.chapterConfig = nil;
function UIDropInfo.Init()
    --todo 应该小写 暂时先大写
    if mapConfig == nil then
        mapConfig = G.dataTable["t_stage"];
        itemConfig = G.dataTable["t_item"];
        dropConfig = G.dataTable["t_stagedrop"];
        equipConfig = dataTable.getDataTable("t_equip");
        chapterConfig = G.dataTable["t_chapter"];

    end

    local name, packageNAme = getUIName(PanelNames.UIDropInfo);
    panelMgr:ShowUI(name, packageNAme, this.onCreate, Config.useLocalUI);
end

function UIDropInfo.onCreate(obj)
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        btnBgClose = obj:GetChild("BgClose");
        btnBackClose = obj:GetChild("btnBack");
        --txtTitle = obj:GetChild("Title");
        --local PlayExpGroup = obj:GetChild("PlayExpGroup");
        --imgPlayExpIcon = PlayExpGroup:GetChild("Icon");
        --txtPlayExpName = PlayExpGroup:GetChild("Name");
        --txtPlayExpOutput = PlayExpGroup:GetChild("Output");
        --local HeroExpGroup = obj:GetChild("HeroExpGroup");
        --imgHeroExpIcon = HeroExpGroup:GetChild("Icon");
        --txtHeroExpName = HeroExpGroup:GetChild("Name");
        --txtHeroExpOutput = HeroExpGroup:GetChild("Output");
        --local GoldGroup = obj:GetChild("GoldGroup");
        --imgGoldIcon = GoldGroup:GetChild("Icon");
        --txtGoldName = GoldGroup:GetChild("Name");
        --txtGoldtOutput = GoldGroup:GetChild("Output");

        listitemGrid = obj:GetChild("chapter"):GetChild("listReward");
        listNextStageItemGrid = obj:GetChild("chapterNext"):GetChild("listReward");
        nextStageTitle = obj:GetChild("n40");
        thisStageTitle=obj:GetChild("title1")
        this.addEvent();
    end
    this.refresh();
end

function UIDropInfo.addEvent()
    btnBgClose.onClick:Add(this.onClose);
    btnBackClose.onClick:Add(this.onClose);
end

function UIDropInfo.removeEvent()
    btnBgClose.onClick:Clear();
    btnBackClose.onClick:Clear();
end

function UIDropInfo.LocalListenEvent()

end

function UIDropInfo.LocalCloseEvent()

end

function UIDropInfo.OnDestroy()
    this.removeEvent();
end

function UIDropInfo.OnEnable()
    this.LocalListenEvent();
end

function UIDropInfo.OnDisable()
    this.LocalCloseEvent();
end

function UIDropInfo.refresh()
    local mapId = logicMgr.MapManager.getNextStage();
    if mapId == 0 then
        mapId = 101;
    elseif mapId == -1 then
        mapId = logicMgr.MapManager.getMapId()
    end
    ----刷新函数
    local curMap = mapConfig[tostring(mapId)];
    --txtTitle.text = curMap.f_ChapterId .. "-" .. curMap.f_StageNum;
    --
    --local playExpConfig = itemConfig[tostring(CurrencyEnum.RoleExp)];
    --imgPlayExpIcon.url = "ui://ItemAtlas/"..playExpConfig.f_Icon;
    ----resMgr:LoadIconFromPrefab("itemAtlas", playExpConfig.f_Icon, function(sp)
    ----    imgPlayExpIcon.sprite = sp;
    ----end);
    --txtPlayExpName.text = getLanguage(playExpConfig.f_ItemName);
    --local power = logicMgr.EcoBuffManager.getEcoBuffByTypeAndItemId(Config.EcoBuffCondition.OffLineRatio, playExpConfig.f_itemModelId);
    --txtPlayExpOutput.text = Mathf.Round(curMap.f_Exp * (1 + power*0.0001)) .. "/m";
    --
    --local HeroExpConfig = itemConfig[tostring(CurrencyEnum.HeroExp)];
    --imgHeroExpIcon.url ="ui://ItemAtlas/".. HeroExpConfig.f_Icon;
    --
    ----resMgr:LoadIconFromPrefab("itemAtlas", HeroExpConfig.f_Icon, function(sp)
    ----    imgHeroExpIcon.sprite = sp;
    ----end);
    --txtHeroExpName.text = getLanguage(HeroExpConfig.f_ItemName);
    --power = logicMgr.EcoBuffManager.getEcoBuffByTypeAndItemId(Config.EcoBuffCondition.OffLineRatio, HeroExpConfig.f_itemModelId);
    --txtHeroExpOutput.text = Mathf.Round(curMap.f_HeroExp * (1 + power*0.0001)) .. "/m";
    --
    --local goldConfig = itemConfig[tostring(CurrencyEnum.MONEY)];
    --imgGoldIcon.url = "ui://ItemAtlas/".. goldConfig.f_Icon;
    ----resMgr:LoadIconFromPrefab("itemAtlas", goldConfig.f_Icon, function(sp)
    ----    imgGoldIcon.sprite = sp;
    ----end);
    --txtGoldName.text = getLanguage(goldConfig.f_ItemName);
    --power = logicMgr.EcoBuffManager.getEcoBuffByTypeAndItemId(Config.EcoBuffCondition.OffLineRatio, goldConfig.f_itemModelId);
    --txtGoldtOutput.text = Mathf.Round(curMap.f_Coin * (1 + power*0.0001)) .. "/m";
    --
    --物品列表
    --local itemDatas = {};
    --local equipLevel = 0;
    --if curMap.f_StageDropId ~= nil and #curMap.f_StageDropId > 0 then
    --    local stageDropList = decodeJsonStrToTabel(curMap.f_StageDropId);
    --    for i = 1, #stageDropList do
    --        local drop = dropConfig[tostring(stageDropList[i])];
    --        local dropList = decodeJsonStrToTabel(drop.f_ItemDropWeight);
    --        for k = 1, #dropList do
    --            local singleDrop = dropList[k];
    --            local itemId = singleDrop[1];
    --            if Mathf.Floor(itemId / 100000) == EnumConst.ItemTypeEnum.Equip then
    --                if equipLevel == 0 then
    --                    local equip = equipConfig[tostring(itemId)];
    --                    if equip ~= nil then
    --                        equipLevel = equip.f_EquipLv;
    --                    end
    --                end
    --            elseif not table.contains(itemId) then
    --                table.insert(itemDatas, itemId);
    --            end
    --        end
    --    end
    --end
    --
    --local itemInfos = {};
    --if equipLevel > 0 then
    --    local data = {};
    --    data.itemModelId = 0;
    --    data.level = equipLevel;
    --    data.icon = "random_level_" .. equipLevel;
    --    table.insert(itemInfos, data);
    --end
    --for i = 1, #itemDatas do
    --    local item = logicMgr.ItemManager.getItemConstDatabyModelId(itemDatas[i]);
    --    if item ~= nil then
    --        item.itemModelId = itemDatas[i];
    --        table.insert(itemInfos, item);
    --    end
    --end
    local itemInfos = {};
    local nextItemInfos = {};

    local curChapter = curMap.f_ChapterId;
    local nextChapter = curChapter + 1;
    if nextChapter > table.getn(chapterConfig) then
        nextChapter = table.getn(chapterConfig);
    end
    local curReward = decodeJsonStrToTabel(chapterConfig[tostring(curChapter)].f_RewardShow);
    local nextReward = decodeJsonStrToTabel(chapterConfig[tostring(nextChapter)].f_RewardShow);

    for i = 1, #curReward do
        local item = logicMgr.ItemManager.getItemConstDatabyModelId(curReward[i]);
        table.insert(itemInfos, item);
    end


    for i = 1, #nextReward do
        local item = logicMgr.ItemManager.getItemConstDatabyModelId(nextReward[i]);
        table.insert(nextItemInfos, item);
    end
    nextStageTitle.text = getLanguage(chapterConfig[tostring(nextChapter)].f_ChapterName);
    thisStageTitle.text=getLanguage(chapterConfig[tostring(curChapter)].f_ChapterName)
    listData = itemInfos;
    listNextData = nextItemInfos;
    listitemGrid.itemRenderer = this.setData;
    listitemGrid.numItems = #listData;
    listNextStageItemGrid.itemRenderer = this.setNextStageData;
    listNextStageItemGrid.numItems = #listNextData;
end

function UIDropInfo.setData(idx, itemobj)
    local itemdata = listData[idx + 1];
    local item = CtrlManager.GetCtrl(PanelNames.Item);
    item.initData(itemobj, itemdata);

    itemobj.onClick:Clear();
    item.initData(itemobj, itemdata);
    if itemdata.itemModelId > 0 then
        itemobj.onClick:Add(function()
            itemdata.num = 0;
            showUI(PanelNames.UIBagItemInfo, itemdata)
        end)
    end
end

function UIDropInfo.setNextStageData(idx, itemobj)
    local itemdata = listNextData[idx + 1];
    local item = CtrlManager.GetCtrl(PanelNames.Item);
    item.initData(itemobj, itemdata);

    itemobj.onClick:Clear();
    item.initData(itemobj, itemdata);
    if itemdata.itemModelId > 0 then
        itemobj.onClick:Add(function()
            itemdata.num = 0;
            showUI(PanelNames.UIBagItemInfo, itemdata)
        end)
    end
end

function UIDropInfo.onClose()
    closeUI(PanelNames.UIDropInfo);
end

return UIDropInfo;
